/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.systemView;
/**
 * 说明：
 * 角色权限管理
 * @Author：高挺
 * @Date：2012-05-04
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;

import com.ruipengkj.bean.TRole;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonGray;
import com.ruipengkj.service.TRoleService;
import com.ruipengkj.view.base.BaseDialog;

public class SystemManagePrivilegeAdd  extends BaseDialog implements Runnable{
	
	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent  nameTF;//角色名称
	private JLabelJTextAreaComponent descTF;//描述
	private JButton sureButton;//确定
	private JButton cancelButton;//取消
	private TRoleService tRoleService;//角色
	public boolean isSuccess=false;
	public SystemManagePrivilegeAdd(JDialog jd, String title, boolean b) {
		super(jd, title, b);
		tRoleService=BuildSpringApplication.getInstance().getTRoleServiceImpl();
		this.setSize(400, 250);
		this.setLocationRelativeTo(null);
		this.init();
		this.initListener();
	}
	private void init() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		nameTF = new JLabelJTextFieldComponent("角色名称：", 40);
		descTF=new JLabelJTextAreaComponent("描    述：", 35, 2);
		JPanel down_panel = new JPanel();
		sureButton = new RPJButtonBlue("确定");
		cancelButton = new RPJButtonGray("取消");
		down_panel.add(sureButton);
		down_panel.add(cancelButton);
		down_panel.setOpaque(false);
		main_jp.add(nameTF);
		main_jp.add(descTF);
		main_jp.add(down_panel);
		
		this.add(main_jp);
	}

	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==sureButton){
			//确定
			String name=nameTF.getText().getText().trim();
			String desc=descTF.getArea().getText();
			if(name.equals("")||name.length()>100){
				JOptionPane.showMessageDialog(null, "角色名称不能为空，且不能大于100个字");
				nameTF.getText().requestFocus();
				return;
			}
			if(desc.equals("")||desc.length()>200){
				JOptionPane.showMessageDialog(null, "角色描述不能为空，且不能大于200个字");
				descTF.getArea().requestFocus();
				return;
			}
			TRole role=new TRole();
			role.setRoleID(UUID.GenUUID15());
			role.setName(name);
			role.setDes(desc);
			try {
				if(tRoleService.findRoleByName(name)!=null){
					JOptionPane.showMessageDialog(null, "该角色名称已经存在，请重新输入");
					return;
				}
				tRoleService.saveService(role);
				this.isSuccess=true;
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("添加角色信息");
				JOptionPane.showMessageDialog(null, "添加成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "添加失败");
			}
			this.dispose();
		}else if(e.getSource()==cancelButton){
			this.dispose();
		}
	}
	@Override
	public void run() {
	}
	
	public void showDialog(){
		this.isSuccess=false;
		SwingUtilities.invokeLater(this);
		this.setVisible(true);
	}

}
